package cn.aaalei.service.impl;

import cn.aaalei.mapper.MainMapper;
import cn.aaalei.pojo.ActivityInfo;
import cn.aaalei.pojo.AutoSignAccount;
import cn.aaalei.pojo.UserInfo;
import cn.aaalei.service.ActionService;
import cn.aaalei.service.MainService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class MainServiceImpl implements MainService {

    @Autowired
    private ActionService actionService;

    @Autowired
    private MainMapper mainMapper;

    /**
     * 获取我参加的活动
     * @param userId
     * @return
     */
    @Override
    public List<ActivityInfo> getMyAttend(String userId) throws Exception {
        String res = actionService.getMyActivity(userId);
        String data = JSONObject.parseObject(res).get("data").toString();
        data = JSONObject.parseObject(data).get("paginateData").toString();
        List<ActivityInfo> activityInfoList = JSONObject.parseArray(data,ActivityInfo.class);
        return activityInfoList;
    }

    /**
     * 获取我账号的头像
     * @param username
     * @return
     */
    @Override
    public UserInfo getMyProfile(String username) {
        return mainMapper.getMyProfile(username);
    }

    @Override
    public List getSignHistory(String activityId, String taskId, String userId) throws Exception {
        String data = JSONObject.parseObject(actionService.getSignHistory(activityId, taskId, userId)).get("data").toString();
        List resList = new ArrayList();
        Map<String,List<Map<String,Object>>> maps = JSONObject.parseObject(data,Map.class);
        Set<String> strings = maps.keySet();
        if(strings.isEmpty()){
            return resList;
        }
        for (String s : strings){
            for (int i = 0;i < maps.get(s).size();i++){
                if(maps.get(s).get(i).get("isRollcall").equals(0)){
                    resList.add(maps.get(s).get(i).get("rollcallId"));
                }
            }
        }
        return resList;
    }

    @Override
    public Integer getAllPeopleNum() {
        return mainMapper.findAllPeopleNum();
    }

    @Override
    public Integer getAllSignLogNum() {
        return mainMapper.findAllSignLogNum();
    }

    @Override
    public Integer getSignLogNumByStudentNo(String studentNo) {
        return mainMapper.findSignLogNumByStudentNo(studentNo);
    }

    @Override
    public AutoSignAccount getAccount(String studentNo) {
        AutoSignAccount account = mainMapper.findAccount(studentNo);
        if(account == null){
            AutoSignAccount newAccount = new AutoSignAccount();
            newAccount.setUsername(studentNo);
            newAccount.setPassword("");
            newAccount.setStartTime("");
            newAccount.setExpireTime("");
            return newAccount;
        }
        return account;
    }


}
